﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Linq;
using System.Xml.Linq;
using System.Data;

public partial class DisplaySurvey2 : System.Web.UI.Page
{
    XmlDocument xmlDoc = new XmlDocument();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindDataFromXml();
        }
        

    }
    public void bindDataFromXml()
    {
        using (DataSet ds = new DataSet())
        {
            ds.ReadXml(Server.MapPath("~/xml/survey2.xml"));
            Repeater1.DataSource = ds;
            Repeater1.DataBind();
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        xmlDoc.Load(Server.MapPath("~/xml/result2.xml"));
        XmlElement user = xmlDoc.CreateElement("user");

        int questionId = 1;
        XmlNodeList xmlNL = xmlDoc.GetElementsByTagName("user");
        int userId = xmlNL.Count + 1;
        XmlAttribute uId = xmlDoc.CreateAttribute("userID");

        uId.Value = userId.ToString();
        foreach (RepeaterItem rItem in Repeater1.Items)
        {
            XmlElement question = xmlDoc.CreateElement("question");
            XmlAttribute quesId = xmlDoc.CreateAttribute("quesId");


            for (int i = 1; i <= 4; i++)
            {
                CheckBox cb = (CheckBox)rItem.FindControl("cbAns" + i.ToString());
                if (cb.Checked)
                {
                    question.InnerText += (i - 1).ToString() + ",";
                    quesId.Value = questionId.ToString();
                }
            }
            question.Attributes.Append(quesId);
            user.AppendChild(question);
            questionId++;
        }

        user.Attributes.Append(uId);
        xmlDoc.DocumentElement.AppendChild(user);
        xmlDoc.Save(Server.MapPath("~/xml/result2.xml"));
        Response.Redirect("InfoSubmit.aspx");

        //Response.Write("<script LANGUAGE='JavaScript' >alert('Submit Successful <\n> Thank you!')</script>");
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("Home2.aspx");
    }
}